package com.junzhiit.nengji.shop.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.bainaeco.framework.base.dao.AbstractBaseDAO;
import com.bainaeco.framework.common.jdbc.StatementParameter;
import com.junzhiit.nengji.shop.bo.ChargeRuleBO;
import com.junzhiit.nengji.shop.dao.ChargeRuleDAO;
import com.junzhiit.nengji.shop.model.ChargeRule;
import com.junzhiit.nengji.shop.model.enums.Is;

@Repository
public class ChargeRuleDAOImpl extends AbstractBaseDAO<ChargeRule> implements ChargeRuleDAO {

	@Override
	public List<ChargeRuleBO> findAll(ChargeRuleBO chargeRuleBO, int pageNumber, int maxPageRow) {
		StringBuffer sql = new StringBuffer();
		StatementParameter param = new StatementParameter();
		sql.append("select cr.id,cr.balance_money,cr.give_integral,cr.give_money,c.name 'coupons_name' from charge_rule cr");
		sql.append(" left join coupons c on c.id = cr.give_coupons_id");
		sql.append(" where cr.is_delete = ? and cr.cs_id = ?");
		
		param.setInt(Is.NO.toIntValue());
		param.setString(chargeRuleBO.getCsId());
		return super.queryForPagingList(sql.toString(),ChargeRuleBO.class, param, pageNumber, maxPageRow);
	}

	@Override
	public int getTotalCount(ChargeRuleBO chargeRuleBO) {
		StringBuffer sql = new StringBuffer();
		StatementParameter param = new StatementParameter();
		sql.append("select count(cr.id) from charge_rule cr");
		sql.append(" left join coupons c on c.id = cr.give_coupons_id");
		sql.append(" where cr.is_delete = ? and cr.cs_id = ?");
		
		param.setInt(Is.NO.toIntValue());
		param.setString(chargeRuleBO.getCsId());
		return super.queryForInt(sql.toString(), param);
	}

	@Override
	public ChargeRuleBO findById(String id) {
		StringBuffer sql = new StringBuffer();
		StatementParameter param = new StatementParameter();
		sql.append("select cr.*,c.name 'coupons_name' from charge_rule cr");
		sql.append(" left join coupons c on c.id = cr.give_coupons_id");
		sql.append(" where cr.is_delete = ? and cr.id = ?");
		
		param.setInt(Is.NO.toIntValue());
		param.setString(id);
		return super.query(sql.toString(),ChargeRuleBO.class, param);
	}

	@Override
	public ChargeRuleBO getRechargeMoney(Double rechargeMoney) {
		StringBuffer sql=new StringBuffer();
		StatementParameter param=new StatementParameter();
		sql.append(" select * from charge_rule where balance_money <= ?  order by balance_money desc limit 1 ");
		param.setDouble(rechargeMoney);
		return super.query(sql.toString(), ChargeRuleBO.class, param);
	}

	@Override
	public List<ChargeRuleBO> findAll(String csId) {
		StringBuffer sql = new StringBuffer();
		StatementParameter param = new StatementParameter();
		sql.append("select cr.id,cr.balance_money,cr.give_integral,cr.give_money,c.name 'coupons_name' from charge_rule cr");
		sql.append(" left join coupons c on c.id = cr.give_coupons_id");
		sql.append(" where cr.is_delete = ? and cr.cs_id = ?");
		sql.append(" order by cr.balance_money ");
		param.setInt(Is.NO.toIntValue());
		param.setString(csId);
		return super.queryForList(sql.toString(),ChargeRuleBO.class, param);
	}

}
